package cn.iocoder.yudao.module.pm.controller.admin.retentionledger;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pm.controller.admin.retentionledger.vo.RetentionLedgerPageReqVO;
import cn.iocoder.yudao.module.pm.controller.admin.retentionledger.vo.RetentionLedgerRespVO;
import cn.iocoder.yudao.module.pm.controller.admin.retentionledger.vo.RetentionLedgerSaveReqVO;
import cn.iocoder.yudao.module.pm.dal.dataobject.retentionledger.RetentionLedgerDO;
import cn.iocoder.yudao.module.pm.service.retentionledger.RetentionLedgerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 留样台账管理")
@RestController
@RequestMapping("/pm/retention-ledger")
@Validated
public class RetentionLedgerController {

    @Resource
    private RetentionLedgerService retentionLedgerService;

    @PostMapping("/create")
    @Operation(summary = "创建留样台账管理")
    @PreAuthorize("@ss.hasPermission('pm:retention-ledger:create')")
    public CommonResult<Long> createRetentionLedger(@Valid @RequestBody RetentionLedgerSaveReqVO createReqVO) {
        return success(retentionLedgerService.createRetentionLedger(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新留样台账管理")
    @PreAuthorize("@ss.hasPermission('pm:retention-ledger:update')")
    public CommonResult<Boolean> updateRetentionLedger(@Valid @RequestBody RetentionLedgerSaveReqVO updateReqVO) {
        retentionLedgerService.updateRetentionLedger(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除留样台账管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:retention-ledger:delete')")
    public CommonResult<Boolean> deleteRetentionLedger(@RequestParam("id") Long id) {
        retentionLedgerService.deleteRetentionLedger(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除留样台账管理")
                @PreAuthorize("@ss.hasPermission('pm:retention-ledger:delete')")
    public CommonResult<Boolean> deleteRetentionLedgerList(@RequestParam("ids") List<Long> ids) {
        retentionLedgerService.deleteRetentionLedgerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得留样台账管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:retention-ledger:query')")
    public CommonResult<RetentionLedgerRespVO> getRetentionLedger(@RequestParam("id") Long id) {
        RetentionLedgerDO retentionLedger = retentionLedgerService.getRetentionLedger(id);
        return success(BeanUtils.toBean(retentionLedger, RetentionLedgerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得留样台账管理分页")
    @PreAuthorize("@ss.hasPermission('pm:retention-ledger:query')")
    public CommonResult<PageResult<RetentionLedgerRespVO>> getRetentionLedgerPage(@Valid RetentionLedgerPageReqVO pageReqVO) {
        PageResult<RetentionLedgerDO> pageResult = retentionLedgerService.getRetentionLedgerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RetentionLedgerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出留样台账管理 Excel")
    @PreAuthorize("@ss.hasPermission('pm:retention-ledger:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRetentionLedgerExcel(@Valid RetentionLedgerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RetentionLedgerDO> list = retentionLedgerService.getRetentionLedgerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "留样台账管理.xls", "数据", RetentionLedgerRespVO.class,
                        BeanUtils.toBean(list, RetentionLedgerRespVO.class));
    }

}